package com.supor.aiot.food.ui.food;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSONObject;
import com.android.baseconfig.base.BaseActivity;
import com.android.baseconfig.base.BasePresenter;
import com.android.baseconfig.common.utils.ToastUtils;
import com.android.baseconfig.common.views.CustomTitleBar;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.app.SuporPresenter;
import com.supor.aiot.common.utils.MarketUtils;
import com.supor.aiot.food.R;
import com.supor.aiot.food.api.SearchFoodImpl;
import com.supor.aiot.food.bean.RecipeHotBean;
import com.supor.aiot.food.bean.RecipeTopicBean;
import com.supor.aiot.food.bean.TopicDetailBean;

import java.util.ArrayList;
import java.util.List;

public class SharedRecipeActivity extends BaseActivity {

    FrameLayout flSharedRoot;
    CustomTitleBar customTitleBar;
    private RecipeTopicBean topicBean;
    private static final String TOPIC_BEAN = "TOPIC_BEAN";
    private List<RecipeHotBean> recipeList = new ArrayList<>();
    private TopicDetailBean topicDetailBean;
    private String dataID;
    private Fragment currentFrgment;
    @Override
    protected int resourceId() {
        return R.layout.activity_shared_recipe;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation

        setContentView(resourceId());
        initView();
        initClassifyData();
    }

    @SuppressLint("AutoDispose")
    private void getTopicDetail(String dataId){
        SearchFoodImpl.getInstance().getTopicDetail(dataId).subscribe(result->{
            if (result.isSuccess()) {
                topicDetailBean=result.data.rows.get(0);
                if (topicDetailBean!=null)initFrament(topicDetailBean.getNote());
            } else {
                Log.e(TAG, "Topic error");
            }

        },throwable -> {
            Log.e(TAG, "Topic查询失败: " + throwable.getMessage());
        });
    }

    /*@SuppressLint("AutoDispose")
    private void getTopicRecipe(String dataId){
        SearchFoodImpl.getInstance().getTopicRecipe(dataId,10).subscribe(result->{
            if (result.isSuccess()) {
                recipeList.clear();
                recipeList.addAll(result.data.rows);
                if (topicDetailBean!=null)initFrament(topicDetailBean.getNote());
            } else {
                Log.e(TAG, "Recipe error");
            }

        },throwable -> {
            Log.e(TAG, "Recipe查询失败: " + throwable.getMessage());
        });
    }*/

    public String getDataID(){
        return topicBean.getDataId();
    }

    private void initFrament(String state) {
        // note属性值："PINTEREST" 瀑布流|"HT" 横向列表|"SHUFFLING" 轮播图
        Log.e(TAG,"state ="+state);
        if (!TextUtils.isEmpty(state)){
            if (state.equals("PINTEREST")){
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.fl_shared_recipe_root, new SharedRecipeOneFragment())
                        .commitNow();
            }else if (state.equals("HT")){
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.fl_shared_recipe_root, new SharedRecipeThreeFragment())
                        .commitNow();
            }else if (state.equals("SHUFFLING")){
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.fl_shared_recipe_root, new SharedRecipeTwoFragment())
                        .commitNow();
            }else{
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.fl_shared_recipe_root, new SharedRecipeTwoFragment())
                        .commitNow();
            }
        }else {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fl_shared_recipe_root, new SharedRecipeTwoFragment())
                    .commitNow();
        }
    }

    @Override
    protected void initView() {
        super.initView();
        flSharedRoot = findViewById(R.id.fl_shared_recipe_root);
        customTitleBar = findViewById(R.id.classify_toolbar);
        if (MarketUtils.getTools().isInstalled(SharedRecipeActivity.this, Constants.App.WeixinPackage)) {
            customTitleBar.setFirstRightIcon(R.mipmap.friend_matrix_mini);
            customTitleBar.setRightIcon(R.mipmap.wechat_mini);
        }

        customTitleBar.setIvLeftOnClickListener(view -> {
            SharedRecipeActivity.this.finish();
        });
        customTitleBar.setIvRightOnClickListener(view -> {
            initShare(1);
        });
        customTitleBar.setIvFirstRightOnClickListener(view -> {
            initShare(2);
        });
    }

    public List<RecipeHotBean> getRecipeLists(){
        return recipeList;
    }

    public TopicDetailBean getTopicDetail(){
        return topicDetailBean;
    }

    private void initClassifyData() {
        topicBean= (RecipeTopicBean) getIntent().getSerializableExtra(TOPIC_BEAN);
        if (topicBean!=null){
            dataID=topicBean.getDataId();
            getTopicDetail(dataID);
        }
    }

    public void showTitle(){
        if (!TextUtils.isEmpty(topicBean.getCuisineTitel()))customTitleBar.setTitle(topicBean.getCuisineTitel());
    }

    public void hideTitle(){
        if (!TextUtils.isEmpty(topicBean.getCuisineTitel()))customTitleBar.setTitle("");
    }

    public void initShare(int shareType){
        String shareUrl = AppAgent.getFoodCollectionShareUrl()+dataID;
        if (!TextUtils.isEmpty(dataID)){
            // // 分享类型：0取消分享；1微信；2微信朋友圈；3 QQ好友；4 QQ空间；5微博；6支付宝；7抖音
            // jo.put(Constants.Key.param_shareType, 1);
            // // 分享方式：0x1-text，0x2-image，0x3-URL
            // jo.put(Constants.Key.param_type, 3);
            JSONObject jo = new JSONObject();
            jo.put(Constants.Key.param_shareType, shareType);
            jo.put(Constants.Key.param_title, topicBean.getCuisineTitel());
            jo.put(Constants.Key.param_desc,topicBean.getCuisineDescribe());
            jo.put(Constants.Key.param_type, 3);
            jo.put(Constants.Key.param_tinyAppShare, 0);
            jo.put(Constants.Key.param_path, shareUrl);
            jo.put(Constants.Key.param_bgImgUrl, topicBean.getCuisineImage());
            SuporPresenter.getInstance().sendShareMsg(jo);
        }else {
            ToastUtils.getInstance().showCustomToast(R.string.check_error);
        }
    }
}
